package com.yiyahui.project.erp.controller;

import java.util.List;

import com.yiyahui.common.utils.poi.ExcelUtil;
import com.yiyahui.framework.aspectj.lang.annotation.Log;
import com.yiyahui.framework.aspectj.lang.enums.BusinessType;
import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.framework.web.page.TableDataInfo;
import com.yiyahui.project.erp.domain.YiyaBillingMain;
import com.yiyahui.project.erp.domain.requestVo.AgainBillingRequest;
import com.yiyahui.project.erp.domain.requestVo.ChargeRecordRequest;
import com.yiyahui.project.erp.domain.requestVo.CreateMainAndItemRequest;
import com.yiyahui.project.erp.service.IYiyaBillingMainService;
import com.yiyahui.project.erp.service.IYiyaMainClosingEntriesService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 主单Controller
 * 
 * @author yiyahui
 * @date 2020-06-10
 */
@Api("主单")
@RestController
@RequestMapping("/erp/main")
public class YiyaBillingMainController extends BaseController
{
    @Autowired
    private IYiyaBillingMainService yiyaBillingMainService;

    @Autowired
    private IYiyaMainClosingEntriesService yiyaMainClosingEntriesService;

    /**
     * 查询主单列表
     */
    @ApiOperation("查询主单列表")
    @PreAuthorize("@ss.hasPermi('erp:main:list')")
    @GetMapping("/list")
    public TableDataInfo list(YiyaBillingMain yiyaBillingMain)
    {
        startPage();
        List<YiyaBillingMain> list = yiyaBillingMainService.selectYiyaBillingMainList(yiyaBillingMain);
        return getDataTable(list);
    }

    /**
     * 导出主单列表
     */
    @ApiOperation("导出主单列表")
    @PreAuthorize("@ss.hasPermi('erp:main:export')")
    @Log(title = "主单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YiyaBillingMain yiyaBillingMain)
    {
        List<YiyaBillingMain> list = yiyaBillingMainService.selectYiyaBillingMainList(yiyaBillingMain);
        ExcelUtil<YiyaBillingMain> util = new ExcelUtil<YiyaBillingMain>(YiyaBillingMain.class);
        return util.exportExcel(list, "main");
    }

    /**
     * 获取主单详细信息
     */
    @ApiOperation("获取主单详细信息")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:main:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yiyaBillingMainService.selectYiyaBillingMainById(id));
    }

    /**
     * 新增主单
     */
    @ApiOperation("新增主单")
    @ApiImplicitParam(name = "yiyaBillingMain", value = "${tableComment}", dataType = "YiyaBillingMain")
    @PreAuthorize("@ss.hasPermi('erp:main:add')")
    @Log(title = "主单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YiyaBillingMain yiyaBillingMain)
    {
        return toAjax(yiyaBillingMainService.insertYiyaBillingMain(yiyaBillingMain));
    }

    /**
     * 修改主单
     */
    @ApiOperation("修改主单")
    @ApiImplicitParam(name = "yiyaBillingMain", value = "${tableComment}", dataType = "YiyaBillingMain")
    @PreAuthorize("@ss.hasPermi('erp:main:edit')")
    @Log(title = "主单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YiyaBillingMain yiyaBillingMain)
    {
        return toAjax(yiyaBillingMainService.updateYiyaBillingMain(yiyaBillingMain));
    }

    /**
     * 删除主单
     */
    @ApiOperation("删除主单")
    @ApiImplicitParam(name = "ids", value = "主键id数组", required = true, dataType = "Long[]", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:main:remove')")
    @Log(title = "主单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yiyaBillingMainService.deleteYiyaBillingMainByIds(ids));
    }


    /**
     * 新增/修改主单和项目
     */
    @ApiOperation("新增/修改主单和项目")
    @Log(title = "主单", businessType = BusinessType.INSERT)
    @PostMapping("/add/main/items")
    public AjaxResult addMainAndItems(@RequestBody CreateMainAndItemRequest createMainAndItemRequest)
    {
        try {
            return yiyaBillingMainService.createMainAndItem(createMainAndItemRequest);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }


    @ApiOperation("再次开单")
    @Log(title = "主单", businessType = BusinessType.INSERT)
    @PostMapping("/again/billing")
    public AjaxResult againBilling(@RequestBody AgainBillingRequest againBillingRequest)
    {
        try {
            return yiyaBillingMainService.againBilling(againBillingRequest);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 收费记录查询
     */
    @ApiOperation("收费记录查询")
    @PostMapping("/charge/record/list")
    public TableDataInfo extendList(@RequestBody ChargeRecordRequest chargeRecordRequest)
    {
        return getDataTableExtend(yiyaBillingMainService.extendList(chargeRecordRequest));
    }

}
